package 链接snappydate数据库相关操作.snappydateDDL相关操作
import org.apache.log4j.Logger

import java.io.{File, FileWriter, PrintWriter}
import java.sql.{DriverManager, PreparedStatement, ResultSet}
import java.text.SimpleDateFormat
import java.util.Date

class Query_Delete {
  def query_delete_table(): Unit = {
    val url: String = s"jdbc:snappydata://172.16.193.60:1527/"
    val conn = DriverManager.getConnection(url)
    val log: Logger = Logger.getLogger("Structure_change_ of_detection_table1.0")
    if (conn == null) {
      log.error("The database is not connect:\n" + conn)
    }
    //    val m_DBMetaData = conn.getMetaData
    /**
     * if delete any table,the ops_monitor.audit_log table will insert one record,status become DETELE
     * Not in the temporary table, but in the log table is the deleted table
     * */
    val sql_c = s"select DISTINCT objectname,schemaname ,objecttype,lastupdatedate,objectcreatescript,status" +
      s" from (select * from " +
      s"(select * from ops_monitor.audit_log where (" +
      s"select Count(1) from ops_monitor.audit_struct_table where audit_struct_table.objectname= audit_log.objectname and " +
      s"audit_struct_table.schemaname= audit_log.schemaname) = 0) order by lastupdatedate desc )" +
      s"order by lastupdatedate desc limit 1"
    val pre_c = conn.prepareStatement(sql_c)
    val rst_c = pre_c.executeQuery()
    while (rst_c.next()) {
      val tableSchema: String = rst_c.getString("schemaname")
      val tableName: String = rst_c.getString("objectname")
      val log_time: String = rst_c.getString("lastupdatedate")
      val status: String = rst_c.getString("status")
      if (status == "delete") {
        //no do thing
        println("no do things.")
        //          Breaks.break()
      } else {
        println("this table is delete............")
        val stmt_c = conn.prepareStatement("INSERT INTO ops_monitor.AUDIT_LOG(schemaname,objecttype,objectname,lastupdatedate," +
          "objectcreatescript,status) VALUES('" + rst_c.getString("schemaname") + "','" +
          rst_c.getString("objecttype") + "','" + rst_c.getString("objectname") +
          "','" + rst_c.getString("lastupdatedate") +
          "','" + rst_c.getString("objectcreatescript") + "','delete')")
        //        log.info(s"The Delete tables,schemaname:${rst_c.getString("schemaname")},objecttype:${rst_c.getString("objecttype")}," +
        //          s"objectname:${rst_c.getString("objectname")}")
        val simpdate = new Date()
        val simpleDateFormattime = new SimpleDateFormat("yyyy-MM-dd")
        val str: String = simpleDateFormattime.format(simpdate)
        val writer = new PrintWriter(new FileWriter(s"/tmp/MonitoringTableStructure/logs/${str}.log",true))
        writer.write(s"${new Date()}:delete tables: schemaname: ${rst_c.getString("schemaname")},objecttype: ${rst_c.getString("objecttype")}," +
          s"objectname: ${rst_c.getString("objectname")}\n")
        writer.close()
        stmt_c.execute()
        stmt_c.close()
      }
    }
    pre_c.close()
    rst_c.close()
    conn.close()

  }
}

object Query_Delete {

}
